﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///OTO 的摘要说明
/// </summary>
public class OTO
{
    public OTO()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public static int? ToInt(object obj, int? v)
    {
        try
        {
            v = int.Parse(obj.ToString());
        }
        catch { }
        return v;
    }

    public static int ToInt(object obj, int v)
    {
        try
        {
            v = int.Parse(obj.ToString());
        }
        catch { }
        return v;
    }

    public static DateTime? ToDate(object obj, DateTime? v)
    {
        try
        {
            v = DateTime.Parse(obj.ToString());
        }
        catch { }
        return v;
    }

    public static DateTime ToDate(object obj, DateTime v)
    {
        try
        {
            v = DateTime.Parse(obj.ToString());
        }
        catch { }
        return v;
    }

    public static decimal? ToDecimal(object obj, decimal? v)
    {
        try
        {
            v = decimal.Parse(obj.ToString());
        }
        catch { }
        return v;
    }

    public static decimal ToDecimal(object obj, decimal v)
    {
        try
        {
            v = decimal.Parse(obj.ToString());
        }
        catch { }
        return v;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="obj">数据源</param>
    /// <param name="a">除数</param>
    /// <param name="b">小数位</param>
    /// <param name="u">单位</param>
    /// <param name="r">替换值</param>
    /// <returns></returns>
    public static string ToDecimal(object obj, int a, int b, string u, string r)
    {
        try
        {
            r = (decimal.Parse(obj.ToString()) / a).ToString("f" + b) + u;
        }
        catch { }
        return r;
    }

    public static string ToStr(object obj)
    {
        string rel = String.Empty;
        try
        {
            rel = obj.ToString();
        }
        catch { }
        return rel;
    }

    public static string ToStr(object obj, string v)
    {
        string rel = "";
        try
        {
            rel = obj.ToString();
        }
        catch { }
        if (rel.Trim().Equals(""))
            rel = v;
        return rel;
    }


    public static string ToDateLine(object obj, int s)
    {
        string rel = "-";
        try
        {
            switch (s)
            {
                case 1:
                    rel = DateTime.Parse(obj.ToString()).ToString("yyyy-MM-dd");
                    break;
                default:
                    break;
            }
        }
        catch
        {
        }
        if (rel == "1900-01-01")
            rel = "-";
        return rel;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="obj">要格式化的值</param>
    /// <param name="n">小数位长度</param>
    /// <param name="v">异常返回值</param>
    /// <returns></returns>
    public static string ToDecLine(object obj, int n, string v)
    {
        string rel = "-";
        try
        {
            rel = decimal.Parse(obj.ToString()).ToString("F" + n);
        }
        catch
        {
        }
        if (rel == "0.00")
            rel = v;
        return rel;
    }

    public static string ToLongLine(object obj, string v)
    {
        string rel = "-";
        try
        {
            rel = long.Parse(obj.ToString()).ToString();
        }
        catch
        {
        }
        if (rel == "0")
            rel = v;
        return rel;
    }

    /// <summary>
    /// ConvertUnit
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string CoUt(object obj, int u, string s)
    {
        string rel = s;
        try
        {
            rel = OTO.ToDecimal(int.Parse(obj.ToString()) / u, 0).ToString("f2");
        }
        catch { }
        return rel;
    }

    public static string CoUt(object obj, int u, string s, int f)
    {
        string rel = s;
        try
        {
            rel = OTO.ToDecimal(int.Parse(obj.ToString()) / u, 0).ToString("f" + f);
        }
        catch { }
        return rel;
    }

    /// <summary>
    /// 红涨绿跌
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="s">异常返回</param>
    /// <param name="u">单位</param>
    /// <returns></returns>
    public static string RedGre(object obj, string s, string u)
    {
        string rel = s;
        try
        {
            decimal d = OTO.ToDecimal(obj, 0);
            if (d > 0)
                rel = "<span class=\"red\">" + obj.ToString() + u + "</span>";
            else if (d == 0)
                rel = "<span >" + s + "</span>";
            else
                rel = "<span class=\"green\">" + obj.ToString() + u + "</span>";
        }
        catch { }
        return rel;
    }


    public static string PerRedGre(object obj, int n, string s, string u)
    {
        string rel = s;
        try
        {
            decimal d = OTO.ToDecimal(obj, 0);
            if (d > 0)
                rel = "<span class=\"red\">" + d.ToString("f" + n) + u + "</span>";
            else if (d == 0)
                rel = "<span >" + s + "</span>";
            else
                rel = "<span class=\"green\">" + d.ToString("f" + n) + u + "</span>";
        }
        catch { }
        return rel;
    }

    /// <summary>
    /// zdf
    /// </summary>
    /// <param name="obj">要格式的数据</param>
    /// <param name="m">被整除</param>
    /// <param name="n">小数位长度</param>
    /// <param name="s">异常替换</param>
    /// <param name="u">单位</param>
    /// <returns></returns>
    public static string PerRedGre(object obj, int m, int n, string s, string u)
    {
        string rel = s;
        try
        {
            decimal d = OTO.ToDecimal(obj, 0);
            if (d > 0)
                rel = "<span class=\"red\">" + (d / m).ToString("f" + n) + u + "</span>";
            else if (d == 0)
                rel = "<span >" + s + "</span>";
            else
                rel = "<span class=\"green\">" + (d / m).ToString("f" + n) + u + "</span>";
        }
        catch { }
        return rel;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="str"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public static string CutStr(string str,int n)
    {
        string rel = str;
        try
        {
            if (str.Length > n)
                rel = str.Substring(0, n);
        }
        catch { }
        return rel;
    }
}